<?php

namespace App\Http\Controllers\Web\Member;

use App\Repository\Models\ActionPermission;
use App\Repository\Models\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Web\BaseController;
use App\Repository\Repositories\Interfaces\UserRepository;
use App\ConstDir\ErrorConst;


class MemberController extends BaseController
{

    protected $user_repo;
    protected $user_id;
    public function __construct(UserRepository $user_repo,Request $request)
    {
        parent::__construct();
        $this->user_repo = $user_repo;
        $this->user_id = array_get($this->user, 'id');
    }

    /**
     * 用户基本信息查询
     *
     */
    public function show($id)
    {
        $user_info = $this->user_repo->with(['address'])->find($id,['id','name','pic','sex','email','pay_pwd','phone','status','identification_type','created_at','is_city_partner','is_test_user','personal_enterprise']);

        return $this->success(ErrorConst::SUCCESS_CODE, $user_info);


    }



    //修改相应信息
    public function update(Request $request , $id)
    {
        $this->validate($request,[
            'pic' => 'required|sometimes|string',
            'sex' => 'required|sometimes|string',
            'pwd' => 'required|sometimes|string',
            'phone' => 'required|sometimes|string',
            'oldphone' => 'required|sometimes|string',
            'email' => 'required|sometimes|email',
            'uptype' => 'required'

        ]);
        $uptype = $request->input('uptype');
        if($uptype == 'upbasic'){
            $data = $request->all('pic','sex');
        }elseif($uptype == 'uppwd'){
            $data = [
                'pwd' => md5($request->input('pwd'))
            ];
        }elseif($uptype == 'upphone'){
            $data = [
                'phone' => $request->input('phone')
            ];

            $user_info = $this->user_repo->find($id,['id','name','phone']);
            if($user_info['data']['name'] == $request->input('oldphone')){
                $data['name'] = $request->input('phone');
            }

        }elseif($uptype == 'uppay'){
            $data = [
                'pay_pwd' => md5($request->input('pay_pwd'))
            ];
        }elseif($uptype == 'upemail'){
            $data = [
                'email' => $request->input('email')
            ];
        }

        $ret = $this->user_repo->update($data, $id);
        if ($ret) {
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, ErrorConst::SUCCESS_CODE);
        } else {
            return $this->error(ErrorConst::FAILED_CODE_MSG);
        }
    }


    /**
     * 店铺认证页面
     */
    public function shop(){

        $user = $this->user_repo->with(['company'=>function($query){
            $query->select(['user_id', 'id','company_name','angent','step']);
        },'company_material'])->find($this->user_id);

//        dd($user);
        return $this->success(ErrorConst::SUCCESS_CODE, $user,true);
    }

    public function shop_auth(){

        $user = $this->user_repo->with(['company'=>function($query){
            $query->select(['user_id', 'id','company_name','angent','step']);
        },'company_material'])->find($this->user_id);

        $data=$user['data'];

        if($data['identification_type']==2 && $data['company_material']){
            $add_data=[
                'sp_name'=>$data['company']['company_name'],
                'sp_uid'=>$this->user_id,
                'sp_cmp_id'=>$data['company']['id'],
                'sp_level'=>1,
            ];
            $shop_id=Shop::query()->create($add_data);
        }else{
            $shop_id=[];
        }

        if($shop_id){
            return $this->success(ErrorConst::SUCCESS_CODE, $shop_id,true);
        }else{
            return $this->error(ErrorConst::FAILED_CODE_MSG);

        }

    }
//
//    /**
//    * 店铺认证
//    *
//    */
//    public function shop()
//    {
//      $user_info = $this->user_repo->with(['address'])->find($id,['id','name','pic','sex','email','pay_pwd','phone','status','identification_type','created_at','is_city_partner','is_test_user','personal_enterprise']);
//
//      return $this->success(ErrorConst::SUCCESS_CODE, $user_info);
//
//
//    }








}
